package com.osfw.framework.core.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.osfw.framework.utils.DateUtils;
import com.osfw.framework.utils.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class BasicEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 搜索值
     */
    @JsonIgnore
    @TableField(exist = false)
    private String searchValue;

    /**
     * 请求参数
     */
    @TableField(exist = false)
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Map<String, Object> params;

    public String getSearchValue() {
        return searchValue;
    }

    public void setSearchValue(String searchValue) {
        this.searchValue = searchValue;
    }

    public Map<String, Object> getParams() {
        if (params == null) {
            params = new HashMap<>();
        }
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public Date getBeginTime() {
        if (params != null && params.containsKey("beginTime") && params.get("beginTime") != null) {
            if (params.get("beginTime") instanceof Date) {
                return (Date) params.get("beginTime");
            } else if (params.get("beginTime") instanceof String && StringUtils.isNotEmpty(params.get("beginTime").toString())) {
                return DateUtils.stringToDate(params.get("beginTime").toString());
            } else if (params.get("beginTime") instanceof Long && ((Long) params.get("beginTime")).longValue() != 0l) {
                return new Date((Long) params.get("beginTime"));
            }
        }
        return null;
    }

    public Date getEndTime() {
        if (params != null && params.containsKey("endTime") && params.get("endTime") != null) {
            if (params.get("endTime") instanceof Date) {
                return (Date) params.get("endTime");
            } else if (params.get("endTime") instanceof String && StringUtils.isNotEmpty(params.get("endTime").toString())) {
                return DateUtils.stringToDate(params.get("endTime").toString());
            } else if (params.get("endTime") instanceof Long && ((Long) params.get("endTime")).longValue() != 0l) {
                return new Date((Long) params.get("endTime"));
            }
        }
        return null;
    }

}
